package beans;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;


import form.NewClientForm;

@Entity
@Table( name = "Client_Info" )
public class ClientInfo implements Serializable {

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private long                    id;

    // Informations existantes sur le formulaire -------------------------------
    private String                  nomFiliale;
    private String                  nomCorrespondant;
    
    private String                  statut;
    private String                  telephone;
    private String                  poste;
    private String                  mail;
    private String                  pays;
    private String                  zone;
    
    private Date               dateEntreeContact;
    private Date               dateProchainContact;
    private Date               dateDernierContact;

    @OneToMany( fetch = FetchType.EAGER )
    @JoinTable( name = "Asso_client_comment", joinColumns = @JoinColumn( name = "client_id" ), inverseJoinColumns = @JoinColumn( name = "comment_id" ) )
    private Collection<Commentaire> listComment = new ArrayList<Commentaire>();
    
    // error when saving. conflict between the two lists.
    // historique will be saved independently from client Info. Optimisation will be done later.
    /*@OneToMany( fetch = FetchType.EAGER )
    @JoinTable( name = "Asso_client_hist", joinColumns = @JoinColumn( name = "client_id" ), inverseJoinColumns = @JoinColumn( name = "hist_id" ) )
    private Collection<HistoriqueFields> listHist = new ArrayList<HistoriqueFields>();*/
    
    @Transient
    private String dateEntree;
    @Transient 
    private String dateProchain;
    @Transient
    private String dateDernier;

    public ClientInfo()
    {
        
    }

    // constructeur de creation de nouveau client ---------------------------
    // ----------------------------------------------------------------------
    public ClientInfo( NewClientForm clientForm ) throws ParseException {

        this.nomFiliale = clientForm.getNomFiliale();
        this.nomCorrespondant = clientForm.getNomCorrespondant();
        this.statut = clientForm.getStatut();
        this.telephone = clientForm.getTelephone();
        this.poste = clientForm.getPoste();
        this.mail = clientForm.getMail();
        this.pays = clientForm.getPays();
        this.zone = clientForm.getZone();
        String dateProchain = clientForm.getDateProchainContact();
        String dateEntree   = clientForm.getDateEntreeContact() ;

        if ( !clientForm.getDateProchainContact().isEmpty() )
        {
            this.dateProchainContact = new SimpleDateFormat("yyyy-MM-dd").parse( dateProchain );
        }
        if ( !clientForm.getDateEntreeContact().isEmpty() )
        {
            this.dateProchainContact = new SimpleDateFormat("yyyy-MM-dd").parse( dateEntree );
        }
    }

   /* public Collection<HistoriqueFields> getListHist() {
        return listHist;
    }

    public void setListHist( Collection<HistoriqueFields> listHist ) {
        this.listHist = listHist;
    }*/

    // getters setters
    // -----------------------------------------------------------------
    // ---------------------------------------------------------------------------------
    public long getId() {
        return id;
    }

    public void setId( long id ) {
        this.id = id;
    }

    public String getNomFiliale() {
        return nomFiliale;
    }

    public void setNomFiliale( String nomFiliale ) {
        this.nomFiliale = nomFiliale;
    }

    public String getNomCorrespondant() {
        return nomCorrespondant;
    }

    public void setNomCorrespondant( String nomCorrespondant ) {
        this.nomCorrespondant = nomCorrespondant;
    }

  
    
    public Date getDateEntreeContact() {
        return dateEntreeContact;
    }

    public void setDateEntreeContact( Date dateEntreeContact ) {
        this.dateEntreeContact = dateEntreeContact;
    }

    public Date getDateProchainContact() {
        return dateProchainContact;
    }

    public void setDateProchainContact( Date dateProchainContact ) {
        this.dateProchainContact = dateProchainContact;
    }

    public Date getDateDernierContact() {
        return dateDernierContact;
    }

    public void setDateDernierContact( Date dateDernierContact ) {
        this.dateDernierContact = dateDernierContact;
    }

    public String getStatut() {
        return statut;
    }

    public void setStatut( String statut ) {
        this.statut = statut;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone( String telephone ) {
        this.telephone = telephone;
    }

    public String getPoste() {
        return poste;
    }

    public void setPoste( String poste ) {
        this.poste = poste;
    }

    public String getMail() {
        return mail;
    }

    public void setMail( String mail ) {
        this.mail = mail;
    }

    public String getPays() {
        return pays;
    }

    public void setPays( String pays ) {
        this.pays = pays;
    }

    public String getZone() {
        return zone;
    }

    public void setZone( String zone ) {
        this.zone = zone;
    }

    public String getDateEntree() {
        return  ( dateEntreeContact==null ? null :  dateEntreeContact.toString().substring( 0, 10 ) );
    }

    public String getDateProchain() {
        return ( dateProchainContact==null ? null :  dateProchainContact.toString().substring( 0,10 ) ) ;
    }

    public String getDateDernier() {
        return ( dateDernierContact==null ? null : dateDernierContact.toString().substring( 0,10 ) ) ;
    }

    public Collection<Commentaire> getListComment() {
        return listComment;
    }

    public void setListComment( Collection<Commentaire> listComment ) {
        this.listComment = listComment;
    }
    
   
    
    


}
